﻿using System;
using System.Windows;
using HBCode.BusinessLogic.Base;
using HBCode.BusinessLogic;
using HBCode.Presentation.Interface;

namespace HBCode.Presentation.Base
{
    public abstract class ViewModelBase : PropertyNotifier, IParentViewModel, IChildViewModel
    {
        #region Members

        private EntityFContext _DbManager;
        /// <summary>
        /// Entity Framework Data Context
        /// </summary>
        public EntityFContext DbManager
        {
            get
            {
                return _DbManager = _DbManager ?? new EntityFContext();
            }
        }

        /// <summary>
        /// return Header in Tab per ViewModel from resources or type name
        /// </summary>
        public string DisplayName
        {
            get
            {
                try
                {
                    var title = System.Windows.Application.Current.TryFindResource(GetType().Name);
                    return title != null ? Convert.ToString(title) : this.GetType().Name;
                }
                catch
                {
                    return "UNDEFINED";
                }
            }
        }

        #endregion

        #region IParent Members

        private IChildViewModel _activeChild;
        public IChildViewModel ActiveChildViewModel
        {
            get
            {
                return _activeChild;
            }
            set
            {
                OnPropertyChanging("ActiveChildViewModel");
                OnPropertyChanging("IsActiveChild");
                OnPropertyChanging("DisplayName");
                if (_activeChild != value)
                {
                    _activeChild = value;
                }
                OnPropertyChanged("ActiveChild");
                OnPropertyChanged("IsActiveChild");
                OnPropertyChanged("ActiveChildViewModel");
            }
        }

        public bool IsActiveChild
        {
            get
            {
                return ActiveChildViewModel != null;
            }
        }

        #endregion

        #region IChild Members

        private IParentViewModel _parent;
        public IParentViewModel Parent
        {
            get
            {
                return _parent;
            }
            set
            {
                if (_parent != value)
                {
                    OnPropertyChanging("Parent");
                    _parent = value;
                    OnPropertyChanged("Parent");
                }
            }
        }

        private bool _isEditing = false;
        /// <summary>
        /// For Editing window is visible or not
        /// </summary>
        public bool IsEditing
        {
            get { return _isEditing; }
            set
            {
                if (_isEditing == value)
                    return;

                OnPropertyChanging("IsEditing");
                _isEditing = value;
                OnPropertyChanged("IsEditing");
            }
        }

        #endregion

        public void ShowMessage(string titleKey, string contentKey, MessageBoxImage messageIcon)
        {
            try
            {
                string _title = titleKey;
                string _content = contentKey;

                object title = System.Windows.Application.Current.TryFindResource(contentKey);
                object content = System.Windows.Application.Current.TryFindResource(titleKey);
                if (title != null)
                    _title = Convert.ToString(title);

                if (content != null)
                    _content = Convert.ToString(content);

                MessageBox.Show(_content, _title, MessageBoxButton.OK, messageIcon, MessageBoxResult.OK);
            }
            catch
            {
                MessageBox.Show(contentKey, titleKey, MessageBoxButton.OK, messageIcon, MessageBoxResult.OK);
            }

        }

        public MessageBoxResult ShowMessageDialog(string titleKey, string contentKey, MessageBoxButton messagebutton, MessageBoxImage messageIcon)
        {
            MessageBoxResult messageresult = MessageBoxResult.No;
            try
            {
                string _title = titleKey;
                string _content = contentKey;

                object title = System.Windows.Application.Current.TryFindResource(contentKey);
                object content = System.Windows.Application.Current.TryFindResource(titleKey);
                if (title != null)
                    _title = Convert.ToString(title);

                if (content != null)
                    _content = Convert.ToString(content);

                messageresult = MessageBox.Show(_title, _content, messagebutton, messageIcon);
            }
            catch { }
            return messageresult;
        }
    }
}
